package com.zf.shuashua.ui.popup;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.core.CenterPopupView;
import com.zf.shuashua.R;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.bean.WithdrawJson2;
import com.zf.shuashua.manager.AdAutoTouchManager;

/**
 * 提现任务-提示3
 */
public class WithdrawTip3Popup extends CenterPopupView {

    private TextView btnTitle, tvMessage1, tvMessage2, tvProgress1, tvProgress2, tvFinish1, tvFinish2;
    private FrameLayout advert_container;
    private ImageView imgClose;
    private OnWithdrawListener mListener;
    private WithdrawJson2 withdrawJson;
    private View view;

    public WithdrawTip3Popup(@NonNull Activity context, WithdrawJson2 withdrawJson) {
        super(context);
        this.withdrawJson = withdrawJson;
    }

    public void addAdvertView(View view) {
        this.view = view;
        advert_container.removeAllViews();
        advert_container.addView(view);
        btnTitle.setVisibility(VISIBLE);
        imgClose.setVisibility(VISIBLE);
    }

    public void loadAdvertFail() {
        advert_container.setVisibility(GONE);
        btnTitle.setVisibility(VISIBLE);
        imgClose.setVisibility(VISIBLE);
    }

    public void setPopupListener(OnWithdrawListener listener) {
        this.mListener = listener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_withdraw_tip3;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        setupView();
        setData();
    }

    private void setupView() {
        btnTitle = findViewById(R.id.btTitle);
        advert_container = findViewById(R.id.advert_container);
        imgClose = findViewById(R.id.img_close);
        tvMessage1 = findViewById(R.id.tvMessage1);
        tvMessage2 = findViewById(R.id.tvMessage2);
        tvProgress1 = findViewById(R.id.tvProgress1);
        tvProgress2 = findViewById(R.id.tvProgress2);
        tvFinish1 = findViewById(R.id.tvFinish1);
        tvFinish2 = findViewById(R.id.tvFinish2);

        btnTitle.setOnClickListener(v -> {
            if (!AdAutoTouchManager.getInstance().randomAutoClickAd(view)) {
                dismiss();
                if (mListener != null) {
                    if (withdrawJson.isIs_finish()) {
                        mListener.onNext();
                    } else {
                        mListener.toFinish(withdrawJson.getValue());
                    }
                }
            }
        });
//        if (Store.getInstance().getTP_AD()) {
//            findViewById(R.id.flCancel).setOnClickListener(v -> {
//                dismiss();
//                if (mListener != null) {
//                    if (withdrawJson.isIs_finish()) {
//                        mListener.onNext();
//                    } else {
//                        mListener.toFinish(withdrawJson.getValue());
//                    }
//                }
//            });
//        } else {
//            btnTitle.setOnClickListener(v -> {
//                dismiss();
//                if (mListener != null) {
//                    if (withdrawJson.isIs_finish()) {
//                        mListener.onNext();
//                    } else {
//                        mListener.toFinish(withdrawJson.getValue());
//                    }
//                }
//            });
//        }
        imgClose.setOnClickListener(v -> {
            dismiss();
        });
    }

    private void setData() {
        if (withdrawJson == null || TextUtils.isEmpty(withdrawJson.getTask())) {
            return;
        }
        String task = withdrawJson.getTask();
        String[] tasks = task.split("-");
        String[] rates = withdrawJson.getRate();
        if (tasks.length >= 2) {
            tvMessage1.setText(tasks[0]);
            String msg2 = tasks[1].replace("或", "");
            tvMessage2.setText(msg2);
            if (rates != null && rates.length >= 2) {
                String[] rate1s = rates[0].split("/");
                try {
                    int rateCount1 = Integer.parseInt(rate1s[0]);
                    int rateCount2 = Integer.parseInt(rate1s[1]);
                    if (rateCount1 >= rateCount2) {
                        tvFinish1.setVisibility(VISIBLE);
                        tvProgress1.setVisibility(GONE);
                    } else {
                        tvFinish1.setVisibility(GONE);
                        tvProgress1.setVisibility(VISIBLE);
                        tvProgress1.setText(rates[0]);
                    }
                } catch (Exception e) {
                    tvFinish1.setVisibility(GONE);
                    tvProgress1.setVisibility(VISIBLE);
                    tvProgress1.setText(rates[0]);
                }

                String[] rate2s = rates[1].split("/");
                try {
                    int rateCount1 = Integer.parseInt(rate2s[0]);
                    int rateCount2 = Integer.parseInt(rate2s[1]);
                    if (rateCount1 >= rateCount2) {
                        tvFinish2.setVisibility(VISIBLE);
                        tvProgress2.setVisibility(GONE);
                    } else {
                        tvFinish2.setVisibility(GONE);
                        tvProgress2.setVisibility(VISIBLE);
                        tvProgress2.setText(rates[1]);
                    }
                } catch (Exception e) {
                    tvFinish2.setVisibility(GONE);
                    tvProgress2.setVisibility(VISIBLE);
                    tvProgress2.setText(rates[1]);
                }
            }
        }
        String title = withdrawJson.isIs_finish() ? "下一步" : "去完成";
        btnTitle.setText(title);
    }

}

